package com.jee.ssm.modules.chargeRules.controller;

import cn.hutool.http.HttpRequest;
import cn.hutool.setting.dialect.Props;
import com.alibaba.fastjson.JSONObject;
import com.jee.ssm.model.ChargeRules;
import com.jee.ssm.model.Park;
import com.jee.ssm.modules.chargeRules.services.ChargeRulesService;
import com.jee.ssm.common.log.AdminControllerLog;
import com.jee.ssm.modules.ssm.controller.AdminBaseController;
import com.jee.ssm.model.json.Tip;
import com.jee.ssm.model.param.ParamMap;
import com.jee.ssm.common.utils.UUIDFactory;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 收费规则管理 Controller
 * @author GaoXiang
 * @version 1.0
 */
@Controller
@RequestMapping("/chargeRules")
public class ChargeRulesController extends AdminBaseController<ChargeRules> {


    /**
     * 进入收费规则添加页面 携带一个生成的id --> longId
     * @param model 返回的实体容器
     * @return 添加页面
     */
    @RequestMapping(value="/add")
    @RequiresPermissions("chargeRules:add")
    public String add(ModelMap model){
        List<Park> parkList = new ArrayList<>();
        try {
            Props props = new Props("config/project.properties");
            String serverUrl = props.getProperty("server.url");
            String method = props.getStr("getRoadsideParking");
            String result = HttpRequest.get(serverUrl + method).execute().body();
            parkList = JSONObject.parseArray(result,Park.class);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("服务器未链接！");
        }
        model.put("parkList",parkList);
        model.put("longId", UUIDFactory.getStringId());
        return "manager/chargeRules/add";
    }


    /**
     * 进入收费规则编辑页面
     * @param model 返回收费规则的容器
     * @param id 收费规则id
     * @return 编辑页面
     */
    @RequestMapping(value="/edit")
    @RequiresPermissions("chargeRules:edit")
    public String edit(ModelMap model, @RequestParam String id) {

        model.put("data",chargeRulesService.selectById(id));
        return "manager/chargeRules/edit";
    }


    /**
     * 收费规则添加
     * @param chargeRules 带id的收费规则对象
     * @return 成功状态
     */
    @RequestMapping(value="/save")
    @ResponseBody
    @RequiresPermissions("chargeRules:add")
    @AdminControllerLog(description = "添加收费规则" )
    public Tip save(ChargeRules chargeRules)  {

        try {
            chargeRules.setCreateTime(new Date());
            chargeRulesService.insert(chargeRules);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1,"添加失败！");
        }

    }


    /**
     * 根据 id 修改收费规则
     * @param chargeRules 带id的收费规则对象
     * @return 成功状态
     */
    @RequestMapping(value="/update")
    @ResponseBody
    @RequiresPermissions("chargeRules:edit")
    @AdminControllerLog(description = "修改收费规则" )
    public Tip update(ChargeRules chargeRules) {
        Tip tip = new Tip();
        try {
            ParamMap map = new ParamMap();
            ChargeRules c = chargeRulesService.selectById(chargeRules.getId());
            map.put("carType",c.getCarType());
            map.put("parkId",c.getParkId());
            List<ChargeRules> chargeRulesList = chargeRulesService.selectByType(map);
            if (chargeRulesList.size() > 0 && chargeRules.getStatus() == 1){
                tip = new Tip(1,"该车型已有规则正在启用中!");
            }else {
                chargeRulesService.updateById(chargeRules);
                tip = new Tip("修改成功!");
            }
        } catch (Exception e) {
            //e.printStackTrace();
            tip = new Tip(1, "修改失败！");
        }
        return tip;

    }


    /**
     * 根据 id 删除收费规则
     * @param id 收费规则id
     * @return 成功状态
     */
    @RequestMapping(value="/delete")
    @ResponseBody
    @RequiresPermissions("chargeRules:delete")
    @AdminControllerLog(description = "删除收费规则" )
    public Tip delete(@RequestParam String id) {

        try {
            chargeRulesService.deleteById(id);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1,"删除失败！");
        }

    }


    /**
     * 根据 id 列表批量删除
     * @param ids 收费规则id List
     * @return 成功状态
     */
    @RequestMapping(value="/deleteByIds")
    @ResponseBody
    @RequiresPermissions("chargeRules:delete")
    @AdminControllerLog(description = "批量删除收费规则" )
    public Tip deleteByIds(@RequestParam("ids") List<String> ids) {

        try {
            chargeRulesService.deleteByIds(ids);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1,"批量删除失败！");
        }

    }


    /**
     * 根据 id 查找收费规则
     * @param id 收费规则id
     * @return 收费规则对象 json
     */
    @RequestMapping(value="/findJson")
    @ResponseBody
    @RequiresPermissions("chargeRules:list")
    public ChargeRules find(@RequestParam String id) {

        return chargeRulesService.selectById(id);
    }
    /**
     * 根据 不同的状态 查找是否存在已启用的收费规则
     * @return 收费规则对象 json
     */
    @RequestMapping(value="/selectByType")
    @ResponseBody
    public Tip selectByType( String buyType,String carType,String parkId) {
        Map map=new HashMap();
        map.put("parkId",parkId);
        if(buyType!=null&&!buyType.equals("")){
            map.put("buyType",buyType);
        }else if(carType!=null&&!carType.equals("")){
            map.put("carType",carType);
        }
        List<ChargeRules> chargeRules=chargeRulesService.selectByType(map);
        if(chargeRules.size()<1){
            return new Tip();
        }else{
            if(buyType!=null&&!buyType.equals("")) {
                if (buyType.equals("1")) {
                    return new Tip(1, "已有按次收费的收费规则在启用中");
                } else if (buyType.equals("0")) {
                    return new Tip(1, "已有按小时收费的收费规则在启用中");
                }
                else if (buyType.equals("2")) {
                    return new Tip(1, "已有日夜分次收费的收费规则在启用中");
                }
            } else if(carType!=null&&!carType.equals("")) {
                if(carType.equals("0")){
                    return new Tip(2, "已有小车的收费规则在启用中");
                }else if(carType.equals("1")){
                    return new Tip(2, "已有大车的收费规则在启用中");
                }
            }
            return new Tip(3,"异常情况");
        }
    }

    /**
     * 根据 id 修改收费规则
     * @return 成功状态
     */
    @RequestMapping(value="/updateChargeRules")
    @ResponseBody
    @RequiresPermissions("chargeRules:edit")
    public Tip updateChargeRules(String id,Integer status) {

        try {
            ChargeRules chargeRules=chargeRulesService.selectById(id);
            chargeRules.setStatus(status);
            chargeRulesService.updateById(chargeRules);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1,"启用失败！");
        }

    }

    /**
     * 获取收费规则列表 获取全部 不分页
     * @param request 请求参数
     * @return 收费规则列表页面
     */
    @RequestMapping(value="/all")
    @RequiresPermissions("chargeRules:list")
    public String all(HttpServletRequest request,ModelMap modelMap) {
        return list(request,modelMap,1,0);
    }


    /**
     * 获取收费规则列表 分页
     * @param request 请求参数
     * @param page 第几页
     * @param size 每页大小
     * @return 收费规则列表页面
     */
    @RequestMapping(value="/list")
    @RequiresPermissions("chargeRules:list")
    public String list(HttpServletRequest request,ModelMap modelMap,Integer page,Integer size) {

        modelMap.put("pageInfo",chargeRulesService.list(new ParamMap(request),page,size));
        modelMap.putAll(new ParamMap(request));
        return "manager/chargeRules/list";
    }


    //---------------------------- property -------------------------------

    @Resource
    private ChargeRulesService chargeRulesService;
}
